<?php

class models_localtion {

    private $_db;
    private $_cache;

    public function __construct($db, $cache) {
        $this->_db = $db;
        $this->_cache = $cache;
    }

    public function get_province_time($province_code) {

        if (isset($province_code)) {
            if ($this->_cache->cache('province_code_' . $province_code)) {
                return $this->_cache->cache('province_code' . $province_code);
            } else {
                $result = $this->_db->selectFirst('location', array('code' => $province_code));

                $exce = $this->_db->getPDOException();
                if ($exce) {
                    throw $exce;
                }

                $this->_cache->cache('province_name' . $province_code, $result['time']);
                return $result['time'];
            }
        }
    }

    public function get_all() {

        if ($this->_cache->cache('time_shipping_all_location')) {
            return $this->_cache->cache('time_shipping_all_location');
        } else {
            $results = $this->_db->select('location');
            $exce = $this->_db->getPDOException();
            if ($exce) {
                throw $exce;
            }

            $this->_cache->cache('time_shipping_all_location', $results);
            return $results;
        }
    }

    public function check_exist($location_code) {
        if ($this->_cache->cache('location_exist_' . $location_code)) {
            return $this->_cache->cache('location_exist_' . $location_code);
        } else {
            $results = $this->_db->selectFirst('location', array('code' => $location_code));
            $this->_cache->cache('location_exist_' . $location_code);
            return $results ? TRUE : FALSE;
        }
    }

}

?>
